//
//  DisplayDetailViewController.swift
//  CoreImageDemo
//
//  Created by wei.feng on 2019/11/29.
//  Copyright © 2019 weifeng. All rights reserved.
//

import UIKit
import SnapKit

class DisplayDetailViewController: UIViewController {

    var filterName : String?
    private var filter : CIFilter?;
    
    var inputImage : UIImage?
    private var outputImage : UIImage?
    
    private lazy var outputImageView = UIImageView(image: outputImage)
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doOutputImage))
        let tipItem = UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: #selector(showDocument))
        self.navigationItem.rightBarButtonItems = [doneItem, tipItem]
        
        let scrollView = UIScrollView()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.backgroundColor = UIColor.white
        self.view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }

        let stackView = UIStackView()
        stackView.backgroundColor = UIColor.white
        stackView.axis = .vertical
        stackView.distribution = .fill
        stackView.alignment = .center
        stackView.spacing = 10.0
        scrollView.addSubview(stackView)
        stackView.snp.makeConstraints { (make) in
            make.leading.top.trailing.bottom.equalToSuperview()
            make.width.equalTo(self.view).priority(.high)
        }
        
        let inputImageView = UIImageView(image: inputImage)
        stackView.addArrangedSubview(inputImageView)
        stackView.addArrangedSubview(outputImageView)
        
        if let name = filterName {
            filter = CIFilter(name: name)
            
            if let f = filter {
                f.setDefaults()
                
                if let a = inputImage, let ciimg = CIImage(image: a) {
                    f.setValue(ciimg, forKey: "inputImage")
                }else{
                    print("have no input image")
                }

                print("attribute \(f.attributes)")
                print("================")
                print("all input keys \(f.inputKeys)")
                print("================")
                print("all output keys \(f.outputKeys)")

            }else{
                print("not a filter named \(name)")
            }
            
        }
    }
    
    @objc func doOutputImage() -> Void {
        if let a = filter?.outputImage {
            let ctx = CIContext(options: nil)
            let cgimg = ctx.createCGImage(a, from: a.extent)
            if let b = cgimg {
                outputImageView.image = UIImage(cgImage: b, scale: inputImage!.scale, orientation: inputImage!.imageOrientation)
                print("生成图片成功")
            }
        }else{
            let alert = UIAlertController(title: "生成图片失败", message: nil, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alert.addAction(cancelAction)
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    @objc func showDocument() -> Void {
        if let f = filter {
            let des = "all input keys: \(f.inputKeys)\n=============\n all output keys \(f.outputKeys)\n=============\n  attribute is :\(f.attributes)"
            let alert : UIAlertController = UIAlertController(title: f.name, message: des, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alert.addAction(cancelAction)
            self.present(alert, animated: true, completion: nil)
        }
    }
}

